package com.qingke.learn;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class LearnReflect {
	public static void main(String[] args){
		Zero zero = new Zero();
		reflect(zero);
//		System.out.println(zero.getNum());
	}
	public static void reflect(Object object){
		//1.
		Class classType1 = object.getClass();
		//2.
		Class classType2 = Zero.class;
		//3.
		try {
			Class classType = Class.forName("Zero");
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		}
		
		//1.
		try {
			Zero newZ = (Zero)classType1.newInstance();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		}
		//2.
		try {
			classType1.getConstructors()[0].newInstance();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (IllegalArgumentException e1) {
			e1.printStackTrace();
		} catch (InvocationTargetException e1) {
			e1.printStackTrace();
		} catch (SecurityException e1) {
			e1.printStackTrace();
		}
		//3.
		try {
			classType1.getConstructors()[1].newInstance(10);
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (IllegalArgumentException e1) {
			e1.printStackTrace();
		} catch (InvocationTargetException e1) {
			e1.printStackTrace();
		} catch (SecurityException e1) {
			e1.printStackTrace();
		}
		
		Field[] field = classType1.getDeclaredFields();
		Field[] allField = classType1.getFields();
		System.out.println("------field------");
		for(Field f:field){
			f.setAccessible(true);
			try {
				System.out.println(f.getName()+":"+f.get(object));
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		System.out.println("------allField------");
		for(Field f:allField){
			f.setAccessible(true);
			try {
				System.out.println(f.getName()+":"+f.get(object));
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		
		System.out.println("------Method------");
		for(Method m:classType1.getDeclaredMethods()){
			System.out.println(m.getName());
			if("setNum".equals(m.getName())){
				m.setAccessible(true);
				try {
					m.invoke(object,100);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println("------allMethod------");
		for(Method m:classType1.getMethods()){
			System.out.println(m.getName());
			if("setNum".equals(m.getName())){
				m.setAccessible(true);
				try {
					m.invoke(object,100);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}
class Zero{
	private int num=0;
	public Zero(){
		
	}
	public Zero(int num){
		this.num=num;
	}
	public int getNum(){
		return num;
	}
	private void setNum(int num){
		this.num=num;
	}
}
